<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="桩编码">
          <a-input placeholder="请输入桩编码" v-decorator="['code', validatorRules.code ]" />
        </a-form-item>
  <!--      <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否删除">
          <a-input placeholder="请输入是否删除" v-decorator="['delFlag', {}]" />
        </a-form-item>-->
       <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="桩编号" >
          <a-input placeholder="请输入桩编号" v-decorator="['pileNumber', validatorRules.pileNumber]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="桩类型">
          <a-select
            style="width: 100%"
            placeholder="请选择桩类型"
            v-decorator="['pileType', validatorRules.pileType]">
            <a-select-option value="1">直流</a-select-option>
            <a-select-option value="2" >交流</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="省份">
          <a-select
            style="width: 100%"
            placeholder="请输入所属省"
            v-decorator="[ 'provinceId', {'initialValue':'17fcef40d4809404c3f4f4806cc05b59'}]">
            <a-select-option v-for="(province,provinceindex) in provinceList" :key="provinceindex.toString()" :value="province.id"  @click="queryCityList(province.id)">
              {{ province.provinceName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="市">
          <!--<a-input placeholder="请输入市" v-decorator="['cityId', {}]" />-->
          <a-select
            style="width: 100%"
            placeholder="请输入所属市"
            v-decorator="[ 'cityId', {'initialValue':'1397aa7b90e48972eb17d9aae32dfd4c'}]">
            <a-select-option v-for="(city,cityindex) in cityList" :key="cityindex.toString()" :value="city.id" @click="queryDistrictList(city.id)">
              {{ city.cityName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="区县">
          <!--<a-input placeholder="请输入区县" v-decorator="['districtId', {}]" />-->
          <a-select
            style="width: 100%"
            placeholder="请输入所属区"
            v-decorator="[ 'districtId', {rules: [{ required: true, message: '请选择区' }]}]">
            <a-select-option v-for="(district,districtindex) in districtList" :key="districtindex.toString()" :value="district.id" @click="siteListByDistrict(district.id)">
              {{ district.districtName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="充电站">
          <!--<a-input placeholder="请选择充电站" v-decorator="['siteId', validatorRules.siteId]" />-->
          <a-select
            style="width: 100%"
            placeholder="请选择充电站"
            v-decorator="[ 'siteId', {rules: [{ required: true, message: '请选择充电站' }]}]">
            <a-select-option v-for="(siteId,siteIdindex) in siteList" :key="siteIdindex.toString()" :value="siteId.id">
              {{ siteId.siteName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <!--<a-form-item-->
          <!--:labelCol="labelCol"-->
          <!--:wrapperCol="wrapperCol"-->
          <!--label="运行状态">-->
          <!--<a-input placeholder="请输入运行状态" v-decorator="['status', {}]" />-->
        <!--</a-form-item>-->
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { postAction } from '@/api/manage'

  export default {
    name: "WxcChargingPileModal",
    data () {
      return {
        title:"操作",
        visible: false,
        provinceList:[],
        cityList:[],
        districtList:[],
        provinceId:"",
        siteList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        code:{rules: [{ required: true, message: '请输入桩编码!' }

            ]},
        pileNumber:{rules: [{ required: true, message: '请输入桩编号!' }]},
        pileType:{rules: [{ required: true, message: '请选择桩类型!' }]},
        siteId:{rules: [{ required: true, message: '请选择充电站!' }]},
        },
        url: {
          add: "/wxcchargingpile/wxcChargingPile/add",
          edit: "/wxcchargingpile/wxcChargingPile/edit",
          provinceListUrl:"/dictionary/dictionary/provinceList",
          cityListUrl:"/dictionary/dictionary/cityList",
          districtListUrl:"/dictionary/dictionary/districtList",
          siteListUrl:"/wxcchargingsite/wxcChargingSite/siteListByDistrict",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.queryProvinceList();
        this.queryCityList('17fcef40d4809404c3f4f4806cc05b59');
        this.queryDistrictList('1397aa7b90e48972eb17d9aae32dfd4c');
        this.siteListByDistrict('90a306fdafc3f4061a9a1854b623f802');
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','pileNumber','pileType','siteId'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      queryProvinceList(){
        postAction(this.url.provinceListUrl).then((res)=>{
          if(res.success){
            this.provinceList = res.result;
          }else{
            console.log(res.message);
          }
        }).catch((err) => {
          this.$message.error("请求异常："+err)
        })
      },
      queryCityList(provinceId){
        let params = {provinceId:provinceId}
        postAction(this.url.cityListUrl,params).then((res)=>{
          if(res.success){
            this.cityList = res.result;
          }else{
            console.log(res.message);
          }
        }).catch((err) => {
          this.$message.error("请求异常："+err)
        })
      },
      queryDistrictList(cityId){
        let params = {cityId:cityId}
        postAction(this.url.districtListUrl,params).then((res)=>{
          if(res.success){
            this.districtList = res.result;
          }else{
            console.log(res.message);
          }
        }).catch((err) => {
          this.$message.error("请求异常："+err)
        })
      },
      siteListByDistrict(districtId){
        let params = {districtId:districtId}
        postAction(this.url.siteListUrl,params).then((res)=>{
          if(res.success){
            this.siteList = res.result;
          }else{
            console.log(res.message);
          }
        }).catch((err)=>{
          this.$message.error("请求异常："+err)
        })

      }
    }
  }
</script>

<style lang="less" scoped>

</style>